package data;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import sprites.Sprite;
import sprites.SpriteMap;
import app.MenuVis;

/**
 * Classe contenente i oggetti grafici.Viene richiamate per refreshare
 * l'immagine
 * 
 * @author TeamVulcan
 */
public class GameLoop extends JPanel implements Observer {

	private static ArrayList<Drawable> drawables;
	private static Image sfondo;

	public GameLoop(ArrayList<Player> players) {
		super();
		drawables = new ArrayList<Drawable>();
		for (int i = 0; i < players.size(); i++) {
			players.get(i).addObserver(this);
			drawables.add(players.get(i));
		}
		
	}

	@Override
	protected void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);
		int c = Game.getSingletonGame().getPlayZone().checkWinner();
		if (c == 0) {
			MenuVis.getVis().drawBG(graphics, GameLoop.this);
			// Actualy draws the Objects
			for (int i = 0; i < drawables.size(); i++) {
				drawables.get(i).draw(graphics);
			}
		} else
			graphics.drawString("Vincitore Team " + c, Game.getSingletonGame()
					.getPlayZone().getWidth() >> 1, Game.getSingletonGame()
					.getPlayZone().getHeight() >> 1);
	}

	/**
	 * @return the drawables
	 */
	public static ArrayList<Drawable> getDrawables() {
		return drawables;
	}

	/**
	 * @param drawables
	 *            the drawables to set
	 */
	public static void setDrawables(ArrayList<Drawable> drawables) {
		GameLoop.drawables = drawables;
	}

	@Override
	public void update(Observable o, Object arg) {
		repaint();
	}

	public static void addDart(Dart dart) {
		drawables.add(dart);
	}
}
